package ar.com.osdepym.agregacionafip.adapters;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import ar.com.osdepym.agregacionafip.activities.R;
import ar.com.osdepym.agregacionafip.activities.context.Results;
import ar.com.osdepym.agregacionafip.exceptions.ResultadoInexistenteException;
import ar.com.osdepym.agregacionafip.modelfile.Resultado;
import ar.com.osdepym.agregacionafip.servicios.ServicioResultados;

public class ListAdapter extends ArrayAdapter<Contenedor>{
	

	    Context context; 
	    int layoutResourceId;    
	    List<Contenedor> data = null;
	    ServicioResultados sr = null;
	    public ListAdapter(Context context, int layoutResourceId, List<Contenedor> data) {
	        super(context, layoutResourceId, data);
	        this.layoutResourceId = layoutResourceId;
	        this.context = context;
	        this.data = data;
	        this.sr = new ServicioResultados();
	    }

	    @Override
	    public View getView(int position, View convertView, ViewGroup parent) {
	        View row = convertView;
	        ContenedorHolder holder = null;
	        
	        if(row == null)
	        {
	            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
	            row = inflater.inflate(layoutResourceId, parent, false);
	            
	            holder = new ContenedorHolder();
	            holder.txtTitle = (TextView)row.findViewById(R.id.itemText);
	            
	            row.setTag(holder);
	        }
	        else
	        {
	            holder = (ContenedorHolder)row.getTag();
	        }
	        
	        Results r = sr.buscarResultados();
	        
	        Contenedor contenedor = data.get(position);
	        try {
				Resultado resultado = r.get(contenedor);
				switch (resultado.getEstado()) {
				case Resultado.PROCESADO_OK:
					row.setBackgroundResource(R.drawable.background_green);	
					break;

				default:
					row.setBackgroundResource(R.drawable.background_red);
					break;
				}
				
			} catch (ResultadoInexistenteException e) {
				row.setBackgroundResource(R.drawable.background_red);
				e.printStackTrace();
			}
	        holder.txtTitle.setText(contenedor.getNombre());
	        
	        return row;
	    }
	    
	    static class ContenedorHolder
	    {
	        TextView txtTitle;
	    }
	}

